package ar.com.fiuba.domain;


/**
 * Concrete strategy to calculate common coefficient. TODO explain
 * 
 */
public class CommonCoefficientCalculationWithPositionStrategy implements CoefficientCalculationStrategy {

	double positionScore = 0.2;

	public Double calculate(Word word, Word anotherWord) {
		int equalNgramms = 0;
		double extraScore = 0;
		Word shortestWord;
		Word largestWord;

		// Set which is the longer word, to iterate over it.
		if (word.getValue().length() > anotherWord.length()) {
			shortestWord = anotherWord;
			largestWord = word;
		} else {
			shortestWord = word;
			largestWord = anotherWord;
		}

		int position = 0;
		// Obtain number of equal ngramms.
		for (Ngramm ngram : shortestWord.getNgramms()) {
			if (largestWord.getNgramms().contains(ngram)) {
				equalNgramms++;
				if (ngram.getValue().equalsIgnoreCase(largestWord.getNgrammList().get(position).getValue()))
					extraScore += positionScore;
			}
			position++;
		}
		return (double) (equalNgramms + extraScore) / (double) largestWord.getNgramms().size();
	}

	public void setPositionScore(double positionScore) {
		this.positionScore = positionScore;
	}

}
